package com.gxuwu.admin.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    private MyUser userInfo;
    //角色+权限集合
    private List<String> permissionValueList;


    public SecurityUser(MyUser userInfo) {
        if (userInfo!=null){
            this.userInfo=userInfo;

        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        for (String permissionValue:permissionValueList){
            if (StringUtils.isEmpty(permissionValue)) continue;
            SimpleGrantedAuthority authority=new SimpleGrantedAuthority(permissionValue);

            authorities.add(authority);
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return userInfo.getPassword();
    }

    @Override
    public String getUsername() {
        return userInfo.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
